//
//  TransferredView.m
//  SP2P
//
//  Created by 吴建东 on 16/6/12.
//  Copyright © 2016年 EIMS. All rights reserved.
//

#import "TransferredView.h"
#import "AddBankCardViewController.h"
#import "RechargeVerifyViewController.h"
#import <FUMobilePay/FUMobilePay.h>
#import "currentShouYiView.h"

@interface TransferredView ()<UIAlertViewDelegate,UITextFieldDelegate> {
    
}

@property (nonatomic ,strong) currentShouYiView *currentView;
@end

@implementation TransferredView
//计算收益
- (IBAction)calculateEarningsclick:(id)sender {
    [AppDelegateInstance.window addSubview:self.currentView]; 
    [UIView animateWithDuration:2.0 animations:^{
        self.currentView.hidden = NO;
    }];
}

- (IBAction)transferredButtonClick:(id)sender {
    
    if (AppDelegateInstance.userInfo.realName.length <= 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您尚未进行实名认证" message:@"是否前去实名认证？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag = 7;
        [alert show];
        return;
    }
    if (AppDelegateInstance.userInfo.hasBindCard == NO) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"您尚未完成实名认证信息" message:@"是否前去完善实名认证信息？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alertView.tag = 14;
        [alertView show];
        return;
    }
    
    if (self.viewType == 1) {
        if (_userBanksDict == nil) {
            [SVProgressHUD showErrorWithStatus:@"您尚未完成实名认证信息"];
            return;
        }
    }
    
    if (_amountTextField.text.doubleValue <= 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的金额"];
        return;
    }
    
    [_amountTextField resignFirstResponder];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"makeSureTransferred" object:[NSString stringWithFormat:@"%ld",self.viewType]];
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
   
    if (alertView.tag==8 && buttonIndex==1) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(gotoSetTradePassword)]) {
            [self.delegate gotoSetTradePassword];
        }
    }else if (alertView.tag==7 && buttonIndex==1) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(gotoRealNameVerify)]) {
            [self.delegate gotoRealNameVerify];
        }
        
    }else if (alertView.tag==14 && buttonIndex==1) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(gotoBindCard)]) {
            [self.delegate gotoBindCard];
        }
    }
}



- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)loadView {
    
    _scrollView.frame = RECTMAKE(0, 0, self.frame.size.width, self.frame.size.height);
       
    
    
    [self initView];
}

- (void)setBalanceStr:(NSString *)balanceStr {
    _balanceStr = balanceStr;
    
    _detailLabel.text = _balanceStr;
}

- (void)setUserBanksDict:(NSDictionary *)userBanksDict {
    _userBanksDict = userBanksDict;
        if (_userBanksDict) {
        NSString *bankNameStr = [NSString stringWithFormat:@"%@",_userBanksDict[@"bankName"]];
        bankNameStr = [CommonClass setBankLogoImageWithBankName:bankNameStr imageView:_logoImageView];
        _nameLabel.text = bankNameStr;
        NSString *bankNumStr = [NSString stringWithFormat:@"%@",_userBanksDict[@"account"]];
        _detailLabel.text = [NSString stringWithFormat:@"尾号%@",[bankNumStr substringFromIndex:bankNumStr.length-4]];
        
    }else {
        
    }
    
}

- (void)textFieldValueChanged {
    if (_amountTextField.text.doubleValue>0) {
        [CommonClass buttonEnableWithButton:_transferredButton enable:YES];
    }else {
        [CommonClass buttonEnableWithButton:_transferredButton enable:NO];
    }
}

- (void)keyboardHidden {
    if (_amountTextField.isFirstResponder) {
        [_amountTextField resignFirstResponder];
    }
    if (_passwordTextField.isFirstResponder) {
        [_passwordTextField resignFirstResponder];
    }
}


#pragma mark - View
- (void)initView {
    self.backgroundColor = KbackgroundColor;
    _nameLabel.font = KFontSize(13);
    _detailLabel.font = KFontSize(13);
    _transAmountValue.font = KFontSize(14);
    _amountTextField.font = KFontSize(14);
    _amountValue.font = KFontSize(14);
    _transferredButton.titleLabel.font = KFontSize(15);
    _remindLabel.font = KFontSize(14);
    if (self.viewType == 1) {
        _nameLabel.text = [NSString stringWithFormat:@""];
        _detailLabel.text = [NSString stringWithFormat:@"%@",@""];
    }else {
        _nameLabel.text = [NSString stringWithFormat:@"账户余额"];
        _detailLabel.text = [NSString stringWithFormat:@"0.00元"];
        _logoImageView.image = [UIImage imageNamed:@"turnout_balance"];
    }
    
    if (AppDelegateInstance.userInfo.isPayPasswordStatus) {
        _passwordTextField.placeholder = @"请输入交易密码";
        _passwordTextField.userInteractionEnabled = YES;
        _passwordTextField.textAlignment = NSTextAlignmentLeft;
    }else {
        _passwordTextField.text = @"请设置交易密码";
        _passwordTextField.userInteractionEnabled = NO;
        _passwordTextField.textAlignment = NSTextAlignmentCenter;
    }

    
    NSString *mindStr = [NSString stringWithFormat:@"%@",@"*转账提示：\n1.每日凌晨两点统一发放前一日利息。\n\n2.单日充值金额超过5万的客户，可在网页版选择网银U盾方式充值。\n\n3.额度表仅作参考，单日限额以各家银行实际额度为准。"];
    CGFloat mindHeight = [MyTools getSizeOfString:mindStr maxWidth:MSWIDTH - 20 maxHeight:1000 withFontSize:14].height;
    CGRect frame = _remindLabel.frame;
    frame.size.height = mindHeight;
    _remindLabel.frame = frame;
    _remindLabel.attributedText = [MyTools getNSMutableAttributedStringWithText:mindStr start:0 end:1 textColor:KColor textFont:KFontSize(14)];
    
   _scrollView.contentSize = CGSizeMake(0, GetMaxY(_remindLabel) + 50);
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldValueChanged) name:UITextFieldTextDidChangeNotification object:_amountTextField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldValueChanged) name:UITextFieldTextDidChangeNotification object:_passwordTextField];
}

- (void)layoutSubviews {
    CGRect frame = _remindLabel.frame;
    frame.origin.y = GetMaxY(_transferredButton) + KAdaptNum(16);;
    _remindLabel.frame = frame;
    
    _scrollView.contentSize = CGSizeMake(0, GetMaxY(_remindLabel) + 50);
}
- (currentShouYiView *)currentView{
    if (_currentView == nil) {
        _currentView = [[currentShouYiView alloc] init];
        _currentView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        _currentView.hidden = YES;
    }
    return _currentView;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
}


@end
